package easycloud.ecs.web.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import easycloud.ecs.common.domain.Folder;
import easycloud.ecs.common.domain.User;
import easycloud.ecs.web.exception.DomainNotFoundException;
import easycloud.ecs.web.service.FolderService;

@Controller
@RequestMapping("folder")
public class FolderController extends BaseController {

	private static final Log logger = LogFactory.getLog(FolderController.class);

	@Inject
	private FolderService folderService;

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public ModelAndView createProcess(
			@ModelAttribute("folder") @Valid Folder folder,
			BindingResult folderBindingResult, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("common.msg");
		if (folderBindingResult.hasErrors()) {
			mav.setViewName("folder.create");
			return mav;
		}
		try {
			User user = getUser(request);
			folder.setUser(user);
			folderService.save(folder);
			mav.addObject("msg", "保存成功");
		} catch (Exception e) {
			mav.addObject("msg", "保存失败");
		}
		return mav;
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public ModelAndView createSetup(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("folder.create");
		Folder folder = new Folder();
		mav.addObject("folder", folder);
		return mav;
	}

	@RequestMapping(value = "delete/{folderId}", method = RequestMethod.GET)
	public ModelAndView deleteProcess(
			@PathVariable("folderId") Integer folderId,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("common.msg");
		Folder folder = folderService.getFolder(folderId);
		User user = getUser(request);
		try {
			if (folder.getUser().equals(user)) {
				folderService.delete(folder);
			}
			mav.addObject("msg", "删除成功");
		} catch (Exception e) {
			mav.addObject("msg", "删除失败");
		}
		return mav;
	}

	@RequestMapping(value = "{folderId}")
	public ModelAndView detail(@PathVariable("folderId") Integer folderId) {
		ModelAndView mav = new ModelAndView("folder.detail");
		Folder folder = folderService.getFolder(folderId);
		if (folder == null) {
			throw new DomainNotFoundException(Folder.class, folderId);
		}

		mav.addObject("folder", folder);
		return mav;
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView listSetup(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("folder.list");
		User user = getUserWithSession(request);
		mav.addObject("folders", user.getFolders());
		return mav;
	}
}
